package com.formiaomiao.demain.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.model.SelectPoiBean;

import java.util.List;

public class SelectPoiAdapter extends RecyclerView.Adapter<SelectPoiAdapter.ViewHolder> {

    Context context;
    List<SelectPoiBean> list;

    public SelectPoiAdapter(Context context, List<SelectPoiBean> list) {
        this.context = context;
        this.list = list;
    }

    public onSelectCheckListener selectCheckListener;

    public interface onSelectCheckListener{
        void selectchecklistener(View v,int index);
    }

    public void setOnSelectCheckListener(onSelectCheckListener selectCheckListener){
        this.selectCheckListener = selectCheckListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.select_poi_item, parent, false);
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        holder.tv_title.setText(list.get(position).getTitle());
        holder.rb_select.setClickable(false);
        if (list.get(position).isIsselect()) {
            holder.rb_select.setChecked(true);
        } else {
            holder.rb_select.setChecked(false);
        }
        if(selectCheckListener!=null){
            holder.rl_select_poi.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectCheckListener.selectchecklistener(v,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        RadioButton rb_select;
        RelativeLayout rl_select_poi;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            rb_select = itemView.findViewById(R.id.rb_select);
            rl_select_poi = itemView.findViewById(R.id.rl_select_poi);
        }
    }

}
